

#ifndef EM_RTC_H__
#define EM_RTC_H__

#include "module_configs/rtc_config.h"

#include <stdint.h> 
#include <stdbool.h> 

#include "rtc.h"

#define EM_RTC_TOMS(x)              ((x * 1000) / CLOCK_SOURCE_FREQ)
#define EM_RTC_TOTICKS(x)           (x * (CLOCK_SOURCE_FREQ/1000))

typedef void (*em_rtc_evt_handle_t)(void); 

typedef struct
{
    uint32_t seconds;
    uint32_t tail;
}em_rtc_time_t;

void em_rtc_init(void);

void em_rtc_update(void); 

uint32_t em_rtc_get_second_ticks(void);

void em_rtc_register_callback( em_rtc_evt_handle_t cb ) ; 

void em_rtc_set_now(datetime_t *p_dt); 

void em_rtc_get_now(datetime_t *p_dt);

uint32_t em_rtc_get_now_timestamp(void); 

uint32_t em_rtc_get_ticks(void);

uint32_t em_rtc_get_diff( uint32_t s, uint32_t e);

/** <brief> calculate time from s tick to end_ticks 
 * <param> s - start tick
 * <param> e - end tick
 * <param> p_rtc_time => seconds & tail
 */ 
void em_rtc_calc_time(uint32_t s, uint32_t e,  em_rtc_time_t * p_rtc_time);

bool em_rtc_check_time_expired( uint32_t s, uint32_t RTC_MS);

uint32_t em_rtc_ticks_to_ms(uint32_t ticks); 

///////////////////////////////////////////////
uint32_t iqw_module_rtc_get_ticks(void);


#endif 

